﻿/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Probel.Ldoc.ViewModels.Dto
{
    using System;
    using System.Windows.Input;

    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.Services.Services;
    using Probel.Ldoc.ViewModels.Pages;
    using Probel.Ldoc.ViewModels.Properties;
    using Probel.Mvvm.DataBinding;
    using Probel.Mvvm.Gui;

    public class EditableDoctorCardViewModel : BaseViewModel
    {
        #region Fields

        private readonly PatientCardDto CurrentPatient;
        private readonly DoctorCardDto Doctor;
        private readonly PatientEditorViewModel ParentViewModel;
        private readonly ICommand removeCommand;
        private readonly IPatientService Service;

        #endregion Fields

        #region Constructors

        public EditableDoctorCardViewModel(PatientEditorViewModel parent, IErrorService errorService, IPatientService service, PatientCardDto patient, DoctorCardDto doctor, bool isAsync = true)
            : base(errorService, isAsync)
        {
            this.removeCommand = new RelayCommand(this.Remove, this.CanRemove);

            this.CurrentPatient = patient;
            this.Doctor = doctor;
            this.Service = service;
            this.ParentViewModel = parent;
        }

        #endregion Constructors

        #region Properties

        public string FirstName
        {
            get { return this.Doctor.FirstName; }
            set
            {
                this.Doctor.FirstName = value;
                this.OnPropertyChanged(() => FirstName);
            }
        }

        public bool IsFemale
        {
            get { return this.Doctor.IsFemale; }
            set
            {
                this.Doctor.IsFemale = value;
                this.OnPropertyChanged(() => IsFemale);
            }
        }

        public string LastName
        {
            get { return this.Doctor.LastName; }
            set
            {
                this.Doctor.LastName = value;
                this.OnPropertyChanged(() => LastName);
            }
        }

        public ICommand RemoveCommand
        {
            get { return this.removeCommand; }
        }

        public SpecialisationDto Specialisation
        {
            get { return this.Doctor.Specialisation; }
            set
            {
                this.Doctor.Specialisation = value;
                this.OnPropertyChanged(() => Specialisation);
            }
        }

        #endregion Properties

        #region Methods

        private bool CanRemove()
        {
            return this.CurrentPatient != null;
        }

        private void Remove()
        {
            var remove = ViewService.MessageBox.Question(string.Format(Messages.Msg_AskRemoveDoctor
                                                       , string.Format("{0} {1}", this.Doctor.FirstName, this.Doctor.LastName)));
            if (remove)
            {
                this.Service.Unbind(this.Doctor, this.CurrentPatient);
                this.ParentViewModel.RefreshDoctors();
            }
        }

        #endregion Methods
    }
}